using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Danser_Control.Managers;
using Danser_Entity.Entities;

namespace Danser_AJAXEnabledWebApplication
{
    /// <summary>
    /// Main/Home Page of Danser application
    /// </summary>
    public partial class Welcome : AbstractPage
    {
        #region Constants:

        private const String NONE_STR = "None";

        #endregion

        #region Page Load:

        /// <summary>
        /// loads the page...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            //Details Panel data updates
            //time and date for the title
            timeLabel.Text = getFormatedServerDateTime();

            //retrieve DB data:
            updateSummationData();
        }

        #endregion

        #region Helper Methods:

        /// <summary>
        /// Perform manual format for the current Date/Time
        /// </summary>
        /// <returns></returns>
        private String getFormatedServerDateTime()
        {
            DateTime dateTime = DateTime.Now;

            return (dateTime.Day / 10 == 0 ? "0" + dateTime.Day.ToString() : dateTime.Day.ToString()) +
                    "/" + (dateTime.Month / 10 == 0 ? "0" + dateTime.Month.ToString() : dateTime.Month.ToString()) +
                    "/"  + dateTime.Year +
                    ", " + (dateTime.Hour / 10 == 0 ? "0" + dateTime.Hour.ToString() : dateTime.Hour.ToString()) +
                    ":"  + (dateTime.Minute / 10 == 0 ? "0" + dateTime.Minute.ToString() : dateTime.Minute.ToString());
        }

        /// <summary>
        /// Retrieve data from DB and plant it in the panel
        /// </summary>
        private void updateSummationData()
        {
            //retrieve data from DB:
            IQueryManager queryManager = ManagerFactory.QueryManager();
            IList<TestInfo> totalTests = queryManager.GetTests(null);

            int totalNum = totalTests.Count;

            int confirmedNum = GetConfirmed(totalTests);
            double confirmedPercentage = Math.Round(((double)confirmedNum / totalNum) * 100, 2);

            int faliedNum = totalNum - confirmedNum;
            double failedPercentage = 100 - confirmedPercentage;

            //get total tests:
            testsNumberLabel.Text = totalNum > 0 ? totalNum.ToString(): NONE_STR;
            //get confirmed tests:
            confirmedNumberLabel.Text = confirmedNum > 0 ? 
                    confirmedNum.ToString() + "\t(" + confirmedPercentage + "%)" : NONE_STR;
            //get failed tests:
            failedNumberLabel.Text = faliedNum > 0 ? 
                    faliedNum.ToString() + "\t(" + failedPercentage + "%)" : NONE_STR;
        }

        /// <summary>
        /// Count the amount of confirmed tests in DB
        /// </summary>
        /// <param name="totalTests">list of all stored tests</param>
        /// <returns>number of confirmed tests</returns>
        private int GetConfirmed(IList<TestInfo> totalTests)
        {
            int counter = 0;

            foreach (TestInfo test in totalTests)
            {
                if (test.IsConfirmed)
                    counter++;
            }
            return counter;
        }

        #endregion

        #region AbstractPage Implementation:

        /// <see cref="Danser_AJAXEnabledWebApplication.AbstractPage.InnerValidatePermission"/>
        protected override bool InnerValidatePermission(Danser_Entity.Entities.PermissionLevel permissionlevel)
        {
            return true;
        }

        #endregion
    }
}
